#include "Echo.hpp"
#include <iostream>
#include "Mytask.hpp"

namespace wd
{
using std::cout;
using std::endl;

Echo::Echo(int threadNum,int taskSize,const string &ip,unsigned short port)
    :_threadpool(threadNum,taskSize)
     ,_server(ip,port)
    {
        using namespace std::placeholders;
        _server.setCallBack(std::bind(&Echo::onConnction,this,_1)
                            ,std::bind(&Echo::onMessage,this,_1)
                            ,std::bind(&Echo::onClosed,this,_1));
    }

void Echo::start()
{
    _threadpool.start();
    _server.start();
}

void Echo:: onConnction(wd::TcpConnectionPtr conn){
    cout<<conn->connectionInfo()<<" has connected"<<endl;
}                                                            

void Echo::onMessage(wd::TcpConnectionPtr conn){
    string msg =conn->receive();

    _threadpool.addTask(wd::TaskPtr(new Mytask(msg,conn)));
}   

void Echo::onClosed(wd::TcpConnectionPtr conn){
    cout<<conn->connectionInfo()<<" has disconnected"<<endl;
}   


}//end of namespace

